package com.student.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.student.DBConn;
import com.student.entities.CommentCount;
import com.student.entities.CommentEntity;
import com.student.entities.UserEntity;

public class CommentService {

	public static List<CommentEntity> getComment(String goods_id, String start,
			String count) throws SQLException {

		List<CommentEntity> commentList = new ArrayList<CommentEntity>();

		Connection conn = DBConn.getConn();
		Statement stmt = conn.createStatement();
		String sql = "select * from shop_comment , shop_user "
				+ "where comment_userId = user_id and        "
				+ "comment_goodsId = " + goods_id
				+ " order by comment_time desc  limit " + start + "," + count;
		ResultSet rs = stmt.executeQuery(sql);
		while (rs.next()) {
			CommentEntity comment = new CommentEntity();
			DateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
			comment.setId(rs.getInt("comment_id"));
			comment.setContent(rs.getString("comment_content"));
			comment.setGoods_id(rs.getInt("comment_goodsId"));
			comment.setRank(rs.getInt("comment_rank"));
			comment.setTime(format.format(rs.getDate("comment_time")));
			comment.setUser_id(rs.getInt("comment_userId"));
			comment.setUser_name(rs.getString("user_name"));
			commentList.add(comment);
		}
		rs.close();
		stmt.close();
		conn.close();
		return commentList;
	}

	public static CommentCount getCommentCount(String goods_id)
			throws SQLException {
		CommentCount comC = new CommentCount();
		Connection conn = DBConn.getConn();
		Statement stmt = conn.createStatement();
		String sql = "select comment_goodsId,comment_rank,count(*) as comment_count from shop_comment  where  comment_goodsId = "
				+ goods_id + " group by comment_rank";

		ResultSet rs = stmt.executeQuery(sql);

		while (rs.next()) {
			switch (rs.getInt("comment_rank")) {
			case 1:
				comC.setStar1(rs.getInt("comment_count"));
				break;
			case 2:
				comC.setStar2(rs.getInt("comment_count"));
				break;
			case 3:
				comC.setStar3(rs.getInt("comment_count"));
				break;
			case 4:
				comC.setStar4(rs.getInt("comment_count"));
				break;
			case 5:
				comC.setStar5(rs.getInt("comment_count"));
				break;
			default:
				break;
			}
			comC.setAll_star();
		}
		rs.close();
		stmt.close();
		conn.close();
		return comC;
	}

	// 添加评论
	public static String addComment(String userName, String goodsId,
			String title, String content, String score) throws SQLException {
		Connection conn = DBConn.getConn();
		Statement stmt = conn.createStatement();
		UserEntity user = UserService.getUser(userName);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		String sql = "INSERT INTO `shop_comment` VALUES (null, '" + goodsId
				+ "', '" + user.getId() + "', '" + title + "', '" + content
				+ "', '" + score + "', '" + sdf.format(new Date()) + "', '0')";

		int rs = stmt.executeUpdate(sql);

		stmt.close();
		conn.close();
		return rs > 0 ? "success" : "failed";
	}

	// 判断用户是否购买某商品
	public static boolean isBuy(String userName, String goodsId) {
		Connection conn = DBConn.getConn();
		Statement stmt;
		boolean result = false;
		try {
			stmt = conn.createStatement();
			String sql = "select * from shop_user,shop_orderitem where item_userId = user_id and user_name =  '"
					+ userName + "' and item_goodsId = " + goodsId;
			ResultSet rs = stmt.executeQuery(sql);
			if (rs.next()) {
				result = true;
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return result;
	}

}
